//
//  TCAlertView.m
//  TCAlertView
//
//  Created by Arthur on 2017/3/23.
//  Copyright © 2017年 Arthur. All rights reserved.
//

#import "TCAlertView.h"
#import "UIView+Frame.h"
#import "KSDatePicker.h"
@interface TCAlertView()

@property (nonatomic, strong) UIView *bgView;

@end

@implementation TCAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 8;
    self.clipsToBounds = YES;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 30)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:14];
    titleLabel.text = @"  来访客户跟进 ";
    [self addSubview:titleLabel];
    
    UILabel *remarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, titleLabel.bottom + 6, 70, 26)];
    remarkLabel.textAlignment = NSTextAlignmentLeft;
    remarkLabel.font = [UIFont systemFontOfSize:14];
    remarkLabel.text = @"跟进日期:";
    [self addSubview:remarkLabel];
    
    self.oneLab = [[UILabel alloc] initWithFrame:CGRectMake(remarkLabel.right , remarkLabel.top, self.width - 30 - remarkLabel.width, 26)];
    self.oneLab.layer.cornerRadius = 5;
    self.oneLab.font = [UIFont systemFontOfSize:12];
    self.oneLab.layer.borderColor = [UIColor grayColor].CGColor;
    self.oneLab.layer.borderWidth = 1.0f;
    [self addSubview:self.oneLab];
    
    UIButton *liBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    liBtn.tag = 7;
  
    liBtn.frame = CGRectMake(self.oneLab.right-35, self.oneLab.top+3, 30, 20);
    [liBtn setBackgroundImage:[UIImage imageNamed:@"btnRiLi"] forState:0];
     [liBtn addTarget:self action:@selector(riLiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:liBtn];
    


    UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, remarkLabel.bottom + 20, 70, 26)];
    msgLabel.text = @"回馈信息:";
    msgLabel.font = [UIFont systemFontOfSize:14];
    msgLabel.textAlignment  = NSTextAlignmentLeft;
    [self addSubview:msgLabel];
    
    
    self.msgField = [[UITextField alloc] initWithFrame:CGRectMake(msgLabel.right , msgLabel.top, self.width-msgLabel.width-30, 26)];
    self.msgField.font = [UIFont systemFontOfSize:14];
    self.msgField.layer.masksToBounds = YES;
    self.msgField.layer.cornerRadius = 4;
    self.msgField.layer.borderColor = [UIColor grayColor].CGColor;
    self.msgField.layer.borderWidth = 0.8;
    [self addSubview:self.msgField];

    
    
    UILabel *dateLab = [[UILabel alloc] initWithFrame:CGRectMake(10, msgLabel.bottom + 20, 100, 26)];
    dateLab.text = @"下次跟进提醒:";
    dateLab.font = [UIFont systemFontOfSize:14];
    dateLab.textAlignment  = NSTextAlignmentLeft;
    [self addSubview:dateLab];

    self.xiaLab = [[UILabel alloc] initWithFrame:CGRectMake(dateLab.right , dateLab.top, self.width - 30 - dateLab.width, 26)];
    self.xiaLab.layer.cornerRadius = 5;
    self.xiaLab.font = [UIFont systemFontOfSize:12];
    self.xiaLab.layer.borderColor = [UIColor grayColor].CGColor;
    self.xiaLab.layer.borderWidth = 1.0f;
    [self addSubview:self.xiaLab];
    
    UIButton *riBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    riBtn.tag = 8;
   
    riBtn.frame = CGRectMake(self.xiaLab.right-35, self.xiaLab.top+3, 30, 20);
    [riBtn setBackgroundImage:[UIImage imageNamed:@"btnRiLi"] forState:0];
    [riBtn addTarget:self action:@selector(riLiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:riBtn];
    

    UIButton *pButton = [UIButton buttonWithType:UIButtonTypeSystem];
    pButton.frame = CGRectMake(60, self.height - 50, 60, 40);
    pButton.backgroundColor = [UIColor orangeColor];
    pButton.layer.cornerRadius = 5;
    [pButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [pButton setTitle:@"保存" forState:UIControlStateNormal];
    [pButton addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:pButton];
    
    
    UIButton *qButton = [UIButton buttonWithType:UIButtonTypeSystem];
    qButton.frame = CGRectMake(self.right-150, pButton.top, 60, 40);
    qButton.backgroundColor = [UIColor orangeColor];
    qButton.layer.cornerRadius = 5;
    [qButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [qButton setTitle:@"取消" forState:UIControlStateNormal];
    [qButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:qButton];

}

#pragma mark --展示view
- (void)showView {
    
  
    _msgField.text = @"";
    
    if (self.bgView) {
        return;
    }
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.bgView addGestureRecognizer:tap];
    
    self.bgView.userInteractionEnabled = YES;
    self.bgView.backgroundColor = [UIColor blackColor];
    self.bgView.alpha =  0.4;
    [window addSubview:self.bgView];
    [window addSubview:self];
}

- (void)tap:(UIGestureRecognizer *)gr {
    
    [self.bgView removeFromSuperview];
    self.bgView = nil;
    [self removeFromSuperview];
}

#pragma mark --关闭view
- (void)closeView {
    [self.bgView removeFromSuperview];
    self.bgView = nil;
    [self removeFromSuperview];
}


- (void)cancelAction:(UIButton *)button {
    [self closeView];
}


- (void)sendAction:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(requestEventAction:)]) {
        [self.delegate requestEventAction:button];
    }
}

-(void)riLiBtnClick:(UIButton *)sender
{
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width - 40, 300)];
    picker.appearance.radius = 5;
    
    //设置回调
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        
        if (buttonType == KSDatePickerButtonCommit) {
            
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            if (sender.tag==7)
            {
                     self.oneLab.text = [formatter stringFromDate:currentDate];
            }
            if (sender.tag == 8)
            {
                self.xiaLab.text =[formatter stringFromDate:currentDate];

            }
                      //[sender setTitle:[formatter stringFromDate:currentDate] forState:UIControlStateNormal];
        }
    };
    // 显示
    [picker show];
}


@end
